<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/CommunicationType.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/RelationType.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/DrinkType.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/RecipeType.php'); 
	
	
	class Lov{
		public static function getRelationTypes() {
			$mysqli = new mysqli('localhost', 'root', 'password', 'ukkepuk');
			$listRelationTypes = $mysqli->prepare("SELECT ID, TYPE FROM relation_type");
			$listRelationTypes->execute();
			$listRelationTypes->bind_result($id, $type);
			
			$result = Array();
			
			while($listRelationTypes->fetch()){
				array_push($result, new RelationType($id, $type));
			}
			
			$mysqli->close();
			return $result;
		}
		
		public static function getCommunicationTypes() {
			$mysqli = new mysqli('localhost', 'root', 'password', 'ukkepuk');
			$listCommunicationTypes = $mysqli->prepare("SELECT ID, TYPE FROM communication_types");
			$listCommunicationTypes->execute();
			$listCommunicationTypes->bind_result($id, $type);
			
			$result = Array();
			
			while($listCommunicationTypes->fetch()){
				array_push($result, new CommunicationType($id, $type));
			}
			
			$mysqli->close();
			return $result;
		}
		
		public static function getDrinkTypes() {
			$mysqli = new mysqli('localhost', 'root', 'password', 'ukkepuk');
			$listDrinkTypes = $mysqli->prepare("SELECT ID, TYPE FROM drink_types");
			$listDrinkTypes->execute();
			$listDrinkTypes->bind_result($id, $type);
			
			$result = Array();
			
			while($listDrinkTypes->fetch()){
				array_push($result, new DrinkType($id, $type));
			}
			
			$mysqli->close();
			return $result;
		}
		
		public static function getRecipies() {
			$mysqli = new mysqli('localhost', 'root', 'password', 'ukkepuk');
			$listRecipe = $mysqli->prepare("SELECT ID, TITLE FROM recipe");
			$listRecipe->execute();
			$listRecipe->bind_result($id, $title);
			
			$result = Array();
			
			while($listRecipe->fetch()){
				array_push($result, new RecipeType($id, $title));
			}
			
			$mysqli->close();
			return $result;
		
		}
		
		
		
		
		public static function getJson($array) {
			//loop over elements in array
			$first = true;
			echo "[";
			foreach($array as $com){
				$reflection = new ReflectionClass($com);
				$refprops = $reflection->getProperties();
				
				$firstprop = true;
				
				if(!$first) {
					echo ",";
				}
					
				echo "{";
				foreach($refprops as $refprop){
					if(!$firstprop){
						echo ",";
					}
					echo "\"" . $refprop->getName() . "\":\"" .  $refprop->getValue($com) . "\"";	
					$firstprop = false;
				}
				
				echo "}";
				$first = false;
			}
			echo "]";
		}
		
		//<select name="$selectId" id="$selectId"><option value="$valuefield">$optionfield</option>
		public static function getSelectionHtml($array, $selectId, $valueField, $optionField){
			echo "<select id=\"$selectId\" name=\"$selectId\">";
				
			foreach($array as $lov){
				$reflection = new ReflectionClass($lov);
				$refprops = $reflection->getProperties();
				
				$val = "";
				$opt = "";
				foreach($refprops as $refprop){
					if($refprop->getName() == $valueField)
						$val = $refprop->getValue($lov);
					if($refprop->getName() == $optionField)
						$opt = $refprop->getValue($lov);
				}
				echo "<option value=\"$val\">$opt</option>";
			}
			echo "</select>";
			
		}
		
	}
	
?>